<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import { getMenuListApi } from '@/api/menu'
import { Menu } from '@/api/menu/types'
import { Refresh, Search } from '@element-plus/icons-vue'
import { ContentWrap } from '@/components/ContentWrap'
import {
  ElInput,
  ElTag,
  ElButton,
  ElTable,
  ElTableColumn,
  ElForm,
  ElRow,
  ElCol,
  ElFormItem
} from 'element-plus'
import Edit from './components/Edit.vue'
import MenuDialogSelect from '@/views/System/compoents/MenuDialogSelect.vue'

const form = reactive({
  name: '',
  status: '',
  roleId: ''
})

const loading = ref(false)
const list = ref<Menu[]>([])
const loadList = async (data) => {
  loading.value = true
  const res = await getMenuListApi(data)
  list.value = res.data
  loading.value = false
}
loadList({})

const refresh = () => {
  form.name = ''
  form.status = ''
  form.roleId = ''
  loadList(form)
}

const search = () => {
  loadList(form)
}

const handleEdit = (index, row) => {
  console.log(index, row)
  const edit = unref(editRef)
  edit?.onUpdate(true, row)
}

const handleDelete = (row) => {
  console.log(row)
}

const editRef = ref<ComponentRef<typeof Edit>>()
const onAdd = () => {
  const edit = unref(editRef)
  edit?.onAdd(true)
}

const menuDialogRef = ref<ComponentRef<typeof MenuDialogSelect>>()
const menuDialog = () => {
  const dialog = unref(menuDialogRef)
  dialog?.onDisabled(true)
}

const menus = ref([])
const printMenus = () => {
  console.log(menus.value)
}
const onConfirm = (data) => {
  console.log(data)
  menus.value = data
}
const onClose = (p1, p2) => {
  console.log('子窗口回调', p1, p2)
  search()
}
</script>

<template>
  <ContentWrap class="flex-[3] ml-10px">
    <el-form ref="formRef" :model="form" label-width="auto" label-position="top">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="菜单名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col>
          <el-button size="small" :icon="Search" @click="search">查询</el-button>
          <el-button size="small" :icon="Refresh" @click="refresh">重置</el-button>
          <el-button size="small" :icon="Refresh" @click="onAdd">新增</el-button>
          <el-button size="small" :icon="Refresh" @click="menuDialog">打开菜单</el-button>
          <el-button size="small" :icon="Refresh" @click="printMenus">输出menus</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      :border="true"
      stripe
      style="width: 100%"
      class="mt-10px"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'child' }"
    >
      <el-table-column type="index" width="45" />
      <el-table-column type="selection" width="40" />
      <el-table-column prop="name" label="菜单名称" width="200" />
      <el-table-column prop="component" label="组件名称" />
      <el-table-column prop="permission" label="权限编码" show-overflow-tooltip width="180" />
      <el-table-column prop="sort" label="顺序" width="100" />
      <el-table-column prop="status" label="状态" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.status != 1" type="info">禁用</el-tag>
          <el-tag v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Operations" width="140" fixed="right">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <!-- :disabled="scope.row.child && scope.row.child.length" -->
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Edit ref="editRef" @onClose="onClose" />

    <MenuDialogSelect :value="menus" ref="menuDialogRef" @onConfirm="onConfirm" />
  </ContentWrap>
</template>
